<?php

class SiteController extends Controller
{
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->render('index');
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}
        
        public function actionRegister()
        {
            $model = new RegistrationForm;
            $new_user = new User;
            
            // if it is ajax validation request
            if(isset($_POST['ajax']) && $_POST['ajax']==='registration-form')
            {
                echo CActiveForm::validate($model);
                Yii::app()->end();
            }

            // collect user input data
            if(isset($_POST['RegistrationForm']))
            {
                $model->attributes=$_POST['RegistrationForm'];
                
                if(!$model->validate())
                {
                    // ViewModel didn't pass validation, return to view while displaying errors
                    $this->render('register',array('model'=>$model));
                    Yii::app()->end();
                }
                
                $new_user->username = $model->username;
                // Add hashed password to new user
                $new_user->password = crypt($model->password, 
                        Randomness::blowfishSalt());
                $new_user->firstname = $model->firstname;
                $new_user->prefix = $model->prefix;
                $new_user->lastname = $model->lastname;
                $new_user->emailaddress = $model->emailaddress;
                // Role is always 'A' for amateurastronoom
                $new_user->role = 'A';

                // save() by default validates the model
                if($new_user->save())
                {
                    // New user added, log new user in.
                    $identity = new UserIdentity(
                            $new_user->username, 
                            $new_user->password);
                    $identity->authenticate();
                    Yii::app()->user->login($identity,0);
                    // Redirect user back to originating page
                    $this->redirect(Yii::app()->user->returnUrl);
                }
            }
            // display the registration form
            $this->render('register',array('model'=>$model));            
        }

	/**
	 * Displays the login page
	 */
	public function actionLogin()
	{
		$model=new LoginForm;

		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='login-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}

		// collect user input data
		if(isset($_POST['LoginForm']))
		{
			$model->attributes=$_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if($model->validate() && $model->login())
				$this->redirect(Yii::app()->user->returnUrl);
		}
		// display the login form
		$this->render('login',array('model'=>$model));
	}

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
}